<?php

namespace Common\Lib\Payments\Yeepay;

use \Common\Lib\Payments\PaymentPlugin;

/**
 * @class Yeepay
 * @brief 易宝支付移动端插件类
 */
class YeepayMobile extends PaymentPlugin {

    public function __construct($payment, $backParam = array()) {
        $backParam['_wap'] = 1;
        parent::__construct($payment, $backParam);
        include_once 'include/yeepayMPay.php';
    }

    /**
     * @see paymentplugin::getSubmitUrl()
     */
    public function getSubmitUrl() {
        
    }

    /**
     * @see paymentplugin::notifyStop()
     */
    public function notifyStop() {
        echo "SUCCESS";
    }


    /**
     * @see paymentplugin::callback()
     */
    public function callback($callbackData) {
        $yeepay = new \yeepayMPay($this->config('M_PartnerId'), $this->config('merchantPublicKey'), $this->config('merchantPrivateKey'), $this->config('yeepayPublicKey'));
        $reply = $this->reply();
        try {
            $return = $yeepay->callback($callbackData['data'], $callbackData['encryptkey']);
            //回传数据
            $reply->orderNo = $return['orderid'];
            $reply->tradeNo = $return['yborderid'];
            $reply->money = $return['amount'] * 100;
            $reply->setStatus($return['status'] == 1);
            return $reply;
        } catch (yeepayMPayException $e) {
            $reply->message = $e->message;
            return $reply;
        }
    }

    /**
     * @see paymentplugin::notifyCallback()
     */
    public function notifyCallback($callbackData) {
        return $this->callback($callbackData);
    }

    /**
     * @see paymentplugin::getSendData()
     */
    public function getSendData($payment) {
        $order_id = $payment['M_OrderNO']; //订单号
        $transtime = time();    //交易时间
        $amount = $payment['M_Amount'] * 100; //交易金额,单位<分>
        $cardno = ''; //默认银行卡号
        $idcardtype = ''; //证件类型
        $idcard = '';   //证件号
        $owner = ''; //持卡人姓名
        $product_catalog = '7';    //产品类型<实物电商>
        $identity_id = str_pad($payment['P_UserId'], 15, 0, STR_PAD_LEFT); //用户身份ID,用户绑定银行卡对应关系<用户ID填充到15位>
        $identity_type = 2; //支付身份标识类型码
        $user_ip = get_client_ip(); //用户IP
        $user_ua = $_SERVER['HTTP_USER_AGENT'];
        $callbackurl = $this->serverCallbackUrl;    //服务器回调
        $fcallbackurl = $this->callbackUrl;     //浏览器回调
        $currency = 156;  //货币<RMB> 156
        $product_name = '升级VIP会员'; //商品名称<固定>
        $product_desc = ''; //商品描述
        $terminaltype = 3;    // 终端标识类型<其他>
        $terminalid = get_client_ip(); //终端标识<就用IP地址>
        $orderexp_date = 3600 * 24 * 7; //订单有效期<7天>
        $yeepay = new \yeepayMPay($payment['M_PartnerId'], $payment['merchantPublicKey'], $payment['merchantPrivateKey'], $payment['yeepayPublicKey']);
        $url = $yeepay->webPay($order_id, $transtime, $amount, $cardno, $idcardtype, $idcard, $owner, $product_catalog, $identity_id, $identity_type, $user_ip, $user_ua, $callbackurl, $fcallbackurl, $currency, $product_name, $product_desc, $terminaltype, $terminalid, $orderexp_date);
        return $url;
    }

    public function doPay($sendData) {
        redirect($sendData);
        exit;
    }

}
